var moment       = require('moment');
var db           = require('../../config/mongo_database');
var logger       = require('../../config/log4js').logger('access');
var property     = require('../../route/property');
var tokenManager = require('../../config/token_manager');

// 我的账单
exports.bill = function(req, res){
	var userid = tokenManager.getUserId(req);
	var where = {userid:userid};
	
	db.balanceModel.count(where, function(err, count){
		db.balanceModel
		.where(where)
		.skip(pages*20)
		.limit(20)
		.sort({ created : -1 })
		.exec(function(err, list){
			res.json({count:count, list:list});
		})
	})
}

// 生成充值对战单
exports.recharge = function(req, res){
	var user      =  tokenManager.getUser(req);
	var detail    =  {}
	detail.btype  =  req.body.btype  || null; // 币种类别
	detail.saddr  =  req.body.saddr  || '';   // 发送地址，有可能是钱包也有可能是自定义
	detail.raddr  =  req.body.raddr  || '';   // 接受送地址
	detail.pay    =  req.body.asset  || 0;    // 金额 
	detail.countn =  detail.pay;              // 金额 
	detail.flag   =  req.body.flag   || '';   // 转入标识

	detail.orderid   = 'c'+moment(new Date()).format("YYYYMMDDHHmmssSS")
	detail.userid    = user.id
	detail.username  = user.username
	detail.nick      = user.nick
	detail.type      = '1'
	detail.content   = '充值服务'

	if(detail.btype == null){
		return res.json({status:400, msg:'钱包不能为空'});
	}
	logger.info('recharge生成充值订单')

	property.addBalance(detail, function(suc){
		if(suc){
			property.findNewWallet(detail) // 查询并新增订单
			return res.json({status:200, msg:'下单成功'});
		}
		else
			return res.json({status:500, msg:'下单失败'});
	})
	
}

// 生成提现对战单-并扣除账户余额
exports.tixian = function(req, res){
	var user      =  tokenManager.getUser(req);
	var detail    =  {}
	detail.btype  =  req.body.btype  || null; // 币种类别
	detail.saddr  =  req.body.saddr  || '';   // 发送地址，有可能是钱包也有可能是自定义
	detail.raddr  =  req.body.raddr  || '';   // 接受送地址
	detail.pay    =  req.body.asset  || 0;    // 金额 
	detail.countn =  detail.pay*(-1);         // 金额 
	detail.flag   =  req.body.flag   || '';   // 转入标识

	detail.orderid   = 'c'+moment(new Date()).format("YYYYMMDDHHmmssSS")
	detail.userid    = user.id
	detail.username  = user.username
	detail.nick      = user.nick
	detail.type      = '2'
	detail.content   = '提现服务'
	detail.ps        = '1'                    // 转出

	if(detail.btype == null){
		return res.json({status:400, msg:'钱包不能为空'});
	}

	if(detail.raddr==null || detail.raddr==''){
		return res.json({status:400, msg:'请输入钱包接收地址'});
	}

	if(detail.pay==null || detail.pay<=0){
		return res.json({status:400, msg:'请输入正确钱包金额'});
	}


	logger.info('recharge生成提现订单')

	// 查询余额是否大于提现
	db.walletModel.findOne({userid:detail.userid, btype:detail.btype}, function(err, wallet){
		if(err || wallet==null){
			return res.json({status:400, msg:'尚未充值该币种钱包，无法提现!'});
		}else{
			if(Number(wallet.asset) < Number(detail.pay)){
				return res.json({status:400, msg:'余额不足，无法提现!'});
			}else{
				// 增加对账单并扣除金额
				property.addBalance(detail, function(suc){
					if(suc){
						property.outWallet(detail) // 扣除钱包余额
						return res.json({status:200, msg:'下单成功'});
					}
					else
						return res.json({status:500, msg:'下单失败'});
				})
			}
		}
	})

}


// 前台用户对账单列表
exports.list = function(req, res){
	var userid = tokenManager.getUserId(req);

	db.balanceModel.find({userid: userid}, function(err, list) {
		res.json({status:200, list:list})
	}).sort({ created : -1 })

}